package com.hl.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.domain.*;
import com.hl.mapper.CourseMapper;
import com.hl.mapper.CoursetypeMapper;
import com.hl.mapper.TeacherMapper;
import com.hl.mapper.UserCourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CourseService {
    @Autowired
    private CourseMapper courseMapper;

    //引入课程类型
    @Autowired
    private CoursetypeMapper coursetypeMapper;

    //引入teacher类型
    @Autowired
    private TeacherMapper teacherMapper;

	@Autowired
    private UserCourseMapper userCourseMapper;
//    通过联表查询所有
    public List<Course> selectAllByAssociation(){
        return courseMapper.selectAllByAssociation();
    }
    public List<Course> selectAllByAssociation2(){
        return courseMapper.selectAllByAssociation2();
    }

    //根据课程id改变状态
    public int modifyStatus(Long id){
        return userCourseMapper.modifyStatus(id);
    }


    public Map<String,Course> selectAll(Integer pageNum,Integer pageSize) {
        // 启动分页
        PageHelper.startPage(pageNum, pageSize);
        List<Course> courseList = courseMapper.selectAll(pageNum, pageSize);
        // 获取分页结果
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);

        Map map = new HashMap<>();
        map.put("list",courseList);
        map.put("total",pageInfo.getTotal());
        return map;
    }


    public Map<String,Course> selectAllSql(String courseName, String type, String consultName, Date createtime,Date updatetime,Integer pageNum,Integer pageSize) {
        // 启动分页
        PageHelper.startPage(pageNum, pageSize);

        List<Course> selectAllSql = courseMapper.selectAllSql(courseName, type, consultName,createtime,updatetime);

        // 获取分页结果
        PageInfo<Course> pageInfo = new PageInfo<>(selectAllSql);

        Map map = new HashMap<>();
        map.put("list",selectAllSql);
        map.put("total",pageInfo.getTotal());
        return map;
    }


    public List<Coursetype> queryAll() {
        return coursetypeMapper.queryAll();
    }

    public List<Teacher> selectAllTeacher() {
        return teacherMapper.selectAllTeacher();
    }


    public int add(Course course) {
        return courseMapper.insertSelective(course);
    }

    //根据id修改课程

    public int update(Course course) {
        return courseMapper.updateByPrimaryKeySelective(course);
    }


    public int delete(Long id) {
        int row = courseMapper.deleteByPrimaryKey(id);
        return row;
    }


    public Map<String, Course> selectCourseLunbo(Integer pageNum, Integer pageSize) {
        // 启动分页
        PageHelper.startPage(pageNum, pageSize);
        List<Course> courses = courseMapper.selectCourseLunbo();
        // 获取分页结果
        PageInfo<Course> pageInfo = new PageInfo<>(courses);

        Map map = new HashMap<>();
        map.put("list",courses);
        map.put("total",pageInfo.getTotal());

        return map;
    }

    //-----------------
    public PageInfo<Course> getCoursesByCondition(String priceRange, String orderBy, String orderDirection,Integer typeId, Integer minReadingQuantity,int pageNum, int pageSize) {
        //创建 Map集合
        Map<String, Object> params = new HashMap<>();

        // 处理价格区间----方便处理
        if (priceRange != null) {
            if ("免费".equals(priceRange)) {
                params.put("isFree", true);
            } else {
                String[] prices = priceRange.split("-");
                if (prices.length == 2) {
                    params.put("minPrice", Integer.parseInt(prices[0]));
                    params.put("maxPrice", Integer.parseInt(prices[1]));
                }
            }
        }

        System.out.println("priceRange"+priceRange);
        // 其他参数处理保持不变
        params.put("orderBy", orderBy);
        params.put("orderDirection", orderDirection);

        if (typeId != null) {
            params.put("typeId", typeId);
        }

        if (minReadingQuantity != null) {
            params.put("minReadingQuantity", minReadingQuantity);
        }

        //启动分页
        PageHelper.startPage(pageNum, pageSize);
        List<Course> courses = courseMapper.selectCoursesByCondition(params);

        return new PageInfo<>(courses);
    }




    //-------------按照条件展示课程数据----
    public List<CourseQuerySort> listCourses(CourseQueryDTO queryDTO) {
        return courseMapper.dynamicQueryCourses(queryDTO);
    }
}
